package com.ksy.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.ksy.common.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;


/**
 * Created by Ksy.
 */

public class PullToRefreshLayout extends SmartRefreshLayout {
    private DragListener dragListener;
    private ClassicsHeader classicsHeader;
    private ClassicsFooter classicsFooter;

    public enum ColorModel {
        Lighter,
        Darker
    }

    public void setDragListener(DragListener dragListener) {
        this.dragListener = dragListener;
    }

    public interface DragListener {
        void drag2Upper(PullToRefreshLayout pullToRefreshLayout);

        void drag2Downer(PullToRefreshLayout pullToRefreshLayout);
    }


    public PullToRefreshLayout(Context context) {
        this(context, null);
    }

    public PullToRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEnableAutoLoadmore(false);
        setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (dragListener != null) {
                    dragListener.drag2Downer(PullToRefreshLayout.this);
                }
            }
        });
        setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (dragListener != null) {
                    dragListener.drag2Upper(PullToRefreshLayout.this);
                }
            }
        });
        setCanRefresh(false);
        setCanLoadMore(false);
        classicsHeader = new ClassicsHeader(getContext());
        classicsFooter = new ClassicsFooter(getContext());
        setRefreshHeader(classicsHeader);
        setRefreshFooter(classicsFooter);
        setEnableOverScrollBounce(false);
        setEnableOverScrollDrag(false);
        setLoaderColorMode(ColorModel.Lighter);
    }

    public void setLoaderColorMode(ColorModel colorMode) {
        if (colorMode == ColorModel.Darker) {
            classicsHeader.setAccentColorId(R.color.common_colorGray);
            classicsHeader.setPrimaryColorId(R.color.common_colorBlack);
            classicsFooter.setAccentColorId(R.color.common_colorGray);
            classicsFooter.setPrimaryColorId(R.color.common_colorBlack);
        } else {
            classicsHeader.setAccentColorId(R.color.common_colorBlack);
            classicsHeader.setPrimaryColorId(R.color.common_colorGray);
            classicsFooter.setAccentColorId(R.color.common_colorBlack);
            classicsFooter.setPrimaryColorId(R.color.common_colorGray);
        }
    }

    public void setCanLoadMore(boolean canLoadMore) {
        setEnableLoadmore(canLoadMore);
    }

    public void setCanRefresh(boolean canLoadMore) {
        setEnableRefresh(canLoadMore);
    }

    @Override
    public SmartRefreshLayout finishRefresh(int delayed, boolean success) {
        return super.finishRefresh(delayed, success);
    }

    private void drag2DownerFinish() {
        finishRefresh();
    }

    private void drag2UpperFinish() {
        finishLoadmore();
    }

    public void dragFinish() {
        drag2DownerFinish();
        drag2UpperFinish();
    }

    private ViewGroup.LayoutParams getParams() {
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return layoutParams;
    }
}
